<template>
  <div>
    <div class="mui-card">
      <router-link to="/user/address/select">
        <!--页眉，放置标题-->
        <div class="mui-card-content bold">
          <span>{{address.name}}</span>
          <span>{{address.tel}}</span>
        </div>
        <!--内容区-->
        <div class="mui-card-content con">
          <span>{{address.area}}</span>
          <span>{{address.detail}}</span>
        </div>
      </router-link>
    </div>

    <div class=" mui-card middle">
      <!-- 商品信息 -->
      <div class="goodsinfo" v-for="item in goodsList" :key="item.id">
        <div class="img-con">
          <img :src="item.image" alt="">
        </div>
        <div class="detail">
          <p class="goodsName">{{item.name}}</p>
          <p class="price">
            ￥{{item.price}}
            <strong class="number">x{{getGoodsCount[item.id]}}</strong>
          </p>
        </div>
        
      </div>
      <!-- 配送信息 -->
      <div class="mui-card-content send">
        <div>
          <strong>配送服务</strong>
          <strong>快递运输</strong>
        </div>
        <div>
          <span>中小件送货时间</span>
          <span>工作日、双休日与节假日均可送货</span>
        </div>
      </div>
      <!-- 订单备注 -->
      <div class="mui-card-content">
        <div class="title">订单备注</div>
        <textarea v-model="note" placeholder="选题，给商家留言"></textarea>
      </div>
    </div>

    <div class="mui-card account">
      <p>
        <i>商品金额</i>
        <strong>￥{{totalPrice}}</strong>
      </p>
      <p>
        <i>运费</i>
        <strong>￥0.00</strong>
      </p>
      <p>
        <s>总价</s>
        <strong>￥{{totalPrice}}</strong>
      </p>
      <button @click="createOrder" class="mui-btn mui-btn-primary">创建订单</button>
    </div>
    
  </div>
</template>

<script>
import { mapState, mapGetters} from 'vuex'
export default {
  data() {
    return {
      address: {},
      addressId: '',
      goodsList: [],
      note: '' // 备注信息
    }
  },
  computed: {
    ...mapState('shopcart',['car', 'buy']),
    ...mapGetters('shopcart', ['getGoodsCount','getSelectedGoods']),
    totalPrice() {
      var total = 0
      this.goodsList.forEach((item,index) => {
        if (this.getSelectedGoods[item.id] && parseInt(item.id) === parseInt(this.getSelectedGoods[item.id].id)) {
          total += this.getSelectedGoods[item.id].count * item.price
        }
      })
      return total.toFixed(2);
    }
  },
  created() {
    if (this.$route.params.id) {
      this.addressId = this.$route.params.id
    }

    // 获取收货地址
    this.getAddress()
    // 获取商品
    this.getGoods()
    // console.log(this.car);
    
  },
  methods: {
    // 获取收货地址
    getAddress() {
      // 如果传了id 就获取对应的收货地址  没有就获取默认的
      this.$http.get('address/def', {params: {id: this.addressId}}).then(res => {
        if (res.data.code === 1) {
          this.address = res.data.data
          // 获取地址id
          this.addressId = this.address.id
        } else if (res.data.code === 2) {
          this.$toast('您未登录，请先登录')
          this.$router.push('/user/login')
        } else {
          this.$router.push('/order/select')
        }
      })
      
    },
    // 获取商品
    getGoods() {
      // 需要获取商品的id
      var ids = [] 
      this.buy.forEach(item => {
        ids.push(item.id)
      })

      this.$http.get('shopcart', {params:{ids}}).then(res => {
        if (res.data.code === 1) {
          this.goodsList = res.data.data
          // console.log(this.goodsList);
        } else {
          this.$toast('商品获取失败')
        }
      })
    },
    // 创建订单
    createOrder() {
      var order = {
        address: this.addressId,
        note: this.note,
        goods: this.buy
      }
      this.$http.post('order/create', order).then(res => {
        if (res.data.code === 1) {
          this.$toast(res.data.msg)
          this.$router.push({name: 'order_list'})
        } else if (res.data.code === 2) {
          this.$toast('您未登录，请先登录')
          this.$router.push('/user/login')
        } else {
          this.$toast('订单创建失败')
        }
      })
      
    }
  }
}
</script>

<style lang="scss" scoped>
.mui-card {
  padding: 15px;
  margin: 0;
  margin-bottom: 10px;
  .bold {
    font-weight: 700;
    font-size: 16px;
    color: #000;
  }
  .con {
    color: #999;
  }
  .mui-card-content {
    padding-bottom: 3px;
    span {
      padding-right: 15px;
    }
  }
  .goodsinfo {
    display: flex;
    border-bottom: 1px solid #ccc;
    padding: 5px 0;
    .img-con {
      width: 70px;
      img {
        width: 100%;
      }
    }
    .detail {
      width: 100%;
      padding-top: 10px;
      padding-left: 10px;
      font-weight: 700;
      .goodsName {
        color: #000;
        
      }
      .price {
        width: 100%;
        font-size: 16px;
        color: red;
      }
    }
    .number {
      float: right;
      font-weight: 400;
      font-size: 14px;
      color: #999;
    }
  }
  .send {
    border-bottom: 1px solid #ccc;
    padding: 10px 0;
    div {
      display: flex;
      justify-content: space-between;
      padding-bottom: 10px;
      span {
        padding-right: 0;
        color: #999;
      }
    }
  }
  .title {
    padding-top: 10px;
    font-weight: 700;
    text-align: center;
  }
  textarea {
    font-size: 14px;
  }
  
}
.middle {
  margin-top: 10px;
  padding-left: 10px;
}
.account {
  p {
    display: flex;
    justify-content: space-between;
    color: #666;
    i {
      font-style: normal;
    }
    strong {
      font-weight: 500;
      color: red;
    }
    s {
      text-decoration: none;
      font-weight: 700;
    }
  }
  button {
    width: 100%;
    height: 45px;
    font-size: 16px;
    margin-top: 10px;
  }
}
</style>